<template>
  <section>
    <el-button size="mini" @click="addActActionBtn">新建活动行为</el-button>
    <el-table :data="actActionList">
      <el-table-column type="index" label="序号"  width="80px"></el-table-column>
      <el-table-column prop="name" label="名称" width="150px">
        <template slot-scope="scope">
          <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
          <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="编码" width="200px">
        <template slot-scope="scope">
          <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.code"></el-input>
          <span  v-show="!scope.row.edit">{{scope.row.code}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
          <el-button @click='cancelBtn(scope.row)' size="mini" v-if="scope.row.edit">取消</el-button>
          <el-button @click='del(scope)' size="mini" v-if="!scope.row.edit">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                   background
                   layout="prev, pager, next"
                   :page-count="totalPage"
                   @current-change="goPage">
    </el-pagination>
  </section>
</template>

<script>
  import errHandler from '@/assets/errHandler'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  export default {
    name: "activityAction",
    components: {},
    data() {
      return {
        totalPage:0,
        actActionList:null
      }
    },
    created(){
      //加载所有活动行为列表
      this.getActivityActionList()
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      goPage(val){
        this.getActivityActionList(val-1)
      },
      getActivityActionList(pageNo){
        this.fetch({name:"getActivityActionList",data:{page:pageNo?pageNo:0}})
          .then(res=>{
            console.log(res)
            this.actActionList=res.content
            this.totalPage=res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addActActionBtn(){
        this.fetch({name:"createActivityAction",data:{name:''}})
          .then(res=>{
            console.log(res)
            res.edit = true
            this.actActionList.unshift(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      confirm(row){
        if(row.edit === undefined){
          Vue.set(row,'edit',true)//后续添加属性，需要vue联动
          return
        }
        if(row.edit){
          this.loading=true
          this.updateActivityAction(row)
        }else {
          row.edit=true
        }
      },
      cancelBtn(row){//取消按钮
        if(row.edit === undefined){
          Vue.set(row,'edit',false)//后续添加属性，需要vue联动
        }else {
          row.edit =false
        }
      },
      del(scope){//删除按钮
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteActivityAction",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.actActionList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateActivityAction(row){
        this.fetch({name:"updateActivityAction",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
